package com.youmingxing.activity.selfInfo;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.adpter.MyFocusAdapter;
import com.youmingxing.bean.selfinfo.FansOrFocus;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.DisplayUtil;
import com.youmingxing.view.DialogManager;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * @author evilyin(ChenZhixi)
 * @since 2015-9-1
 */

public class MyFocusActivity extends BaseActivity implements RefreshListView.OnLoadMoreListener {

    RefreshListView mListMyFocus;
    MyFocusAdapter adapter;
    List<FansOrFocus> mList;
    int mark = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_list_view);
        setTopbarName(R.string.myfocus);
        setLeftAndBtn(true, false, null, null);
        mList = new ArrayList<>();
        mListMyFocus = (RefreshListView) findViewById(R.id.list_view);
        DialogManager.showDialog(this);
        SelfInfoApi.getMyFocus(mark, focusHandler);
    }

    @Override
    public void onErrorClick() {
        SelfInfoApi.getMyFocus(mark, focusHandler);
    }

    private void initView() {
        mListMyFocus.setOnLoadMoreListener(this);
        adapter = new MyFocusAdapter(this);
        mListMyFocus.setAdapter(adapter);
    }

    @Override
    public void onLoadMore() {
        SelfInfoApi.getMyFocus(mark, focusHandler);
    }

    BaseJsonHandler focusHandler = new BaseJsonHandler(this){
        @Override
        public void onSuccessWithData(JSONObject response) {
            showErrorPage(false);
            if (adapter == null){
                initView();
            }
            mListMyFocus.endLoadMore();
            if (response.optInt("status") == 200) {
                JSONArray array = response.optJSONArray("data");
                if (mark == 1 && array.length()==0){
                    View v = View.inflate(MyFocusActivity.this,R.layout.footer_no_data_focus,null);
                    v.setLayoutParams(new AbsListView.LayoutParams(DisplayUtil.getScreenWidth
                            (),DisplayUtil.getScreenHeightWithoutHeader()));
                    ((ImageView)v.findViewById(R.id.hint_img)).setImageResource(R.mipmap.none_focus);
                    ((TextView)v.findViewById(R.id.hin1)).setText("您还没有关注任何人");
                    ((TextView)v.findViewById(R.id.hin2)).setText("");
                    mListMyFocus.noData(v);
                } else if (array.length()<20){
                    mListMyFocus.noMoreData();
                }
                mark++;
                for (int i = 0; i < array.length(); i++) {
                    FansOrFocus bean = new FansOrFocus(array.optJSONObject(i));
                    mList.add(bean);
                }

            }
            adapter.setData(mList);
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            mListMyFocus.endLoadMore();
            showErrorPage(true);
        }
    };
}
